<?php
/**
 * Created by PhpStorm.
 * User: 4399-0661
 * Date: 15-3-21
 * Time: 下午3:48
 */

namespace test;


use Library\template\LibParse;
use Library\template\LibTools;

class LibraryTest extends \PHPUnit_Framework_TestCase
{
    public function testTools()
    {
        $result = \LibTools::readConfig();
        print_r($result);
    }

    public function testSource()
    {
        $ctlBase = new \Application\Base\source\ctlBase();
        $ctlBase->funcIndex();
    }

    public function testDatabase()
    {
        $database = new \LibDatabase();
        //$result = $database->fetch("user" , "*" , array("id"=>1,"username"=>"daqiang"),"username desc","","","username");
        $result = $database->insert("user", array("username" => "3"));
        print_r($result);
    }

    public function testCount()
    {
        $database = new \LibDatabase();
        $result = $database->count("user", 1);
    }

    public function testDelete()
    {
        $database = new \LibDatabase();
        $result = $database->delete("user", array("id" => 2));
        print_r($result);
    }

    public function testInsert()
    {
        $database = new \LibDatabase();
        $datas = [
            ["username" => 5],
            ["username" => 6],
            ["username" => 7],
            ["username" => 8]
        ];
        $database->multiInsert("user", $datas);
    }

    public function testParseParams()
    {
        $string = '<{ $template.log.ext}>';
        preg_match('/(?<=\<\{)(\s*\$[\w|\.]+\s*)(?=\}\>)/is', $string, $result);
        print_r($result[0]);
    }

    public function testParseFunc()
    {
        $string = '    <{switch name="$template.log.sfff"}>
    <{case value="55" }>
        dddddddddd
    <{/case}>
    <{case value="ff"}>
        ffffffffff
    <{/case}>
    <{default}>
        defaultdefaultdefault
    <{/default}>
    <{/switch}>';
        $result = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($result);
    }

    public function testParseLang()
    {
        $string = '<{lang 用户名}>';
        $result = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($result);
    }

    public function testParseImg()
    {
        $string = "<{img images/youbi.jpg}>";
        $result = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($result);
    }

    public function testParseFor()
    {
        $string = '<{for start=0 end=count($array) step=1}>fsdfsdf<{/for}>';
        $result = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($result);
    }

    public function testParseOperate()
    {
        $string = '<{$start+1}>';
        $result = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($result);
    }

    public function testConvert()
    {
        $params = "         dddddd     ffff      ff

        fff
            erwer



            ff";

        $params = preg_replace(array('/\s{1,}/is', '/\r\n/is'), " ", $params);
        print_r($params);
    }

    public function testReplace()
    {
        $content = ' <?php  <script type="text/javascript" src="/base/css/css.js"></script> ?>';
        $content = preg_replace('/(\<\?php\s*)(?=\s*\<script)/is', "", $content);
        $content = preg_replace('/((?<=\<\/script\>)(\s*\?\>))/is', "", $content);
        print_r($content);
    }

    /*
    if (preg_match('/^\$[\w|\.|\s]+\s*\?\s*[\s\$\w\s]*\s*:\s*[\s\$\w\s]+$/is', $params) !== false) {   //三元运算符
        $params = preg_replace_callback('/\$[^\?\:]+/', function ($match) {
            return $this->_convertParams($match[0], false);
        }, $params);
        $params = "echo " . $params . ";";
        return $params;
    }
    */

    public function testExists()
    {
        $string = '<{assign name="var" value="$var.php.data1"}>';
        $string = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($string);
    }

    public function testTemplateImg()
    {
        $string = '<img src="<{img images/1.png dir=\'manager\'}>"></a>';
        $string = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($string);
    }

    public function testTemplateFile()
    {
        $string = '<{file static/upload/1.png app="manager"}>';
        $string = \LibTemplate::getInstance()->_parse("Base", $string);
        print_r($string);
    }

} 

